﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace simulate_control.Basic
{
    #region USING
    using simulate_control.Repositories;
    using simulate_control.Models;
    using DevComponents.DotNetBar;
    using simulate_control.Helpers;
    #endregion

    public partial class FrmMainMenu : Form
    {
        #region VARIABLES
        public EventHandler Handler { get; set; }
        private const int OFFY = 29;
        private const int FIRSTX = 26;
        private const int SECONDX = 248;
        private int FIRSTY = 63;
        private int Selected;
        private List<MainMenu> lst;
        #endregion

        #region METHODS
        public FrmMainMenu()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            lst = MainMenuRepository.SelectAll();
            if (lst.Count > 0)
            {
                for (int i = 0; i < lst.Count; i++)
                {
                    LabelX lbl = new LabelX
                    {
                        BackColor = System.Drawing.Color.Transparent,
                        Name = "lbl" + i,
                        Size = new System.Drawing.Size(163, 23),
                        TabIndex = i,
                        Text = " " + lst[i].Name
                    };

                    if (i % 2 == 0)
                    {
                        lbl.Location = new System.Drawing.Point(FIRSTX, FIRSTY);
                        if (i == 0)
                        {
                            lbl.BackColor = System.Drawing.Color.Crimson;
                            lbl.ForeColor = System.Drawing.Color.White;
                            Selected = 0;
                        }
                    }
                    else
                    {
                        lbl.Location = new System.Drawing.Point(SECONDX, FIRSTY);
                        if (i >= 1)
                        {
                            FIRSTY += OFFY;
                        }
                    } 
                    
                    pnlMainMenu.Controls.Add(lbl);
                }
            }
        }

        public void ProcessKeyEvent(KEY keyEvent)
        {
            if (keyEvent == KEY.ENTER)
            {
                var selectedMenu = SelectItem(Selected);
                if (selectedMenu != null)
                {
                    this.Handler(this, new TransferDataEvent(selectedMenu.ID + "#" + selectedMenu.Name));
                }
            }
            else
            {
                switch (keyEvent)
                {
                    case KEY.UP:
                        if (Selected - 2 >= 0)
                        {
                            Selected -= 2;
                        }
                        break;
                    case KEY.DOWN:
                        if (Selected + 2 < lst.Count)
                        {
                            Selected += 2;
                        }
                        break;
                    case KEY.RIGHT:
                        if (Selected % 2 == 0 && Selected < lst.Count - 1)
                        {
                            Selected += 1;
                        }
                        break;
                    case KEY.LEFT:
                        if (Selected % 2 != 0)
                        {
                            Selected -= 1;
                        }
                        break;
                }
                MoveItem(Selected);
            }          
        }

        public MainMenu SelectItem(int selected)
        {
            if (lst.Count > 0)
            {
                foreach (var item in lst)
                {
                    foreach (var ctrl in pnlMainMenu.Controls)
                    {
                        if (ctrl.GetType() == typeof(LabelX))
                        {
                            var lbl = (LabelX)ctrl;
                            if (lbl.Name.StartsWith("lbl"))
                            {
                                if (lbl.Name == ("lbl" + selected) && lbl.Text.Trim() == item.Name.Trim())
                                {
                                    return item;
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }

        public void MoveItem(int selected)
        {
            foreach (var item in pnlMainMenu.Controls)
            {
                if (item.GetType() == typeof(LabelX))
                {
                    var lbl = (LabelX)item;
                    if (lbl.Name.StartsWith("lbl"))
                    {
                        if (lbl.Name == ("lbl" + selected))
                        {
                            lbl.BackColor = System.Drawing.Color.Crimson;
                            lbl.ForeColor = System.Drawing.Color.White;
                        }
                        else
                        {
                            lbl.BackColor = System.Drawing.Color.Transparent;
                            lbl.ForeColor = SystemColors.ControlText;
                        }
                    }
                }
            }
        }

        private void FrmMainMenu_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion
    }
}
